﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PeopleFinderBusiness
{
	internal class Logger
	{
		internal static void WriteLog(LogTypeEnum type, string clientIP, string message, int? idUser)
		{
			DatabaseDataContext bd = new DatabaseDataContext();
			string typeName = string.Empty;
			switch(type)
			{
				case LogTypeEnum.UserCreation:
					typeName = "User creation";
					break;
				case LogTypeEnum.UserDeleted:
					typeName = "User delete";
					break;
				case LogTypeEnum.UserFailedAutentication:
					typeName = "Failed authentication";
					break;
				case LogTypeEnum.ApplicationError:
					typeName = "Application error";
					break;
			}
			LogType typeDB = (from p in bd.LogType where p.TypeName == typeName select p).First();
			Log newLog = new Log(){
				ClientIP = clientIP,
				LogMessage = message,
				IdUser = idUser,
				LogType = typeDB
			};
			bd.Log.InsertOnSubmit(newLog);
			bd.SubmitChanges();
		}
	}

	enum LogTypeEnum
	{
		ApplicationError,
		UserCreation,
		UserDeleted,
		UserFailedAutentication
	}
}
